package com.app.teaware.login;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.app.teaware.bean.BaseListData;
import com.app.teaware.bean.BaseListDataListBean;
import com.app.teaware.bean.LoginServiceListData;
import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.util.MyUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;

import butterknife.BindView;
import butterknife.OnClick;
import my.CheckUtils;
import my.ViewUtils;


public class SeverSettingActivity extends BasisActivity {

    @BindView(R.id.edit_ip)
    EditText edit_ip;
    @BindView(R.id.edit_port)
    EditText edit_port;
    @BindView(R.id.text_servername)
    TextView text_servername;
    @BindView(R.id.view_edit_ip)
    View view_edit_ip;
    BaseListData severDataCurrent;


    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.login_service_setting_activity);

//        setTitleBg(R.drawable.base_transparent);
        setTitleLeftButton(null);
        setTitle("参数设置");
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        if(!TextUtils.isEmpty(MyUtils.getIp())){
            severDataCurrent =getSeverDefaultListBean().getDataById(MyUtils.getIp());
            if(severDataCurrent == null){
                severDataCurrent = new BaseListData(MyUtils.getIp(),"自定义服务器");
            }
        }else {
            severDataCurrent = getSeverDefaultListBean().getList().get(0);
        }
        onSeverChoosed(severDataCurrent);

    }


    @OnClick(R.id.btn_ok)
    void checkAndPost() {
        String ip = edit_ip.getText().toString().trim();

        if (CheckUtils.isStrEmpty(ip)) {
            showToast(edit_ip.getHint().toString());
            return;
        }

        if(ip.contains(":")){
            String port = ip.substring(ip.lastIndexOf(":")+1);
            try {
                int postInt = Integer.valueOf(port);
                if(postInt > 65535){
                    showToast("请输入正确的端口号");
                    return;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }


        LoginServiceListData loginServiceListData = new LoginServiceListData();
        loginServiceListData.setIp(ip);
//        loginServiceListData.setPort(port);
        loginServiceListData.setName("默认");
        loginServiceListData.save();
        MyUtils.putSerciceData(loginServiceListData);
        setResult(Activity.RESULT_OK);
        finish();
    }

    public static final BaseListDataListBean getSeverDefaultListBean(){
        BaseListDataListBean listBean = new BaseListDataListBean();
        BaseListData baseListData;
        baseListData = new BaseListData("http://121.41.112.69:9296","正式服务器");
        listBean.getList().add(baseListData);
        baseListData = new BaseListData("http://192.168.1.108:9296","赖总");
        listBean.getList().add(baseListData);
        return listBean;
    }

    @OnClick(R.id.view_server)
    void onServerChooseClick(){
        BaseListDataListBean listBean = getSeverDefaultListBean();
        listBean.getList().add(new BaseListData("","自定义服务器"));
        final String[] items = listBean.getListStringGroup();
        BaseListData baseListDataCurrent = new BaseListData(edit_ip.getText().toString().trim(),"");
        int checkedIndex = -1;
        checkedIndex = listBean.getCheckedPosition(baseListDataCurrent);
        if(checkedIndex == -1){
            checkedIndex = 3;
        }

        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setTitle(text_servername.getHint() + "")
                .setCheckedIndex(checkedIndex)
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        onSeverChoosed(listBean.getList().get(which));
                        dialog.dismiss();
                    }
                })
                .create().show();
    }

    void onSeverChoosed(BaseListData baseListData){
        if(TextUtils.isEmpty(baseListData.getId()) || baseListData.getName().equals("自定义服务器")){
            view_edit_ip.setVisibility(View.VISIBLE);
            edit_ip.setText(baseListData.getId());
            ViewUtils.setEditTextEnable(true,edit_ip);
        }else {
//            view_edit_ip.setVisibility(View.GONE);
            view_edit_ip.setVisibility(View.GONE);
            edit_ip.setText(baseListData.getId());
            ViewUtils.setEditTextEnable(false,edit_ip);
        }
        text_servername.setText(baseListData.getName());
    }

}
